package com.army.study;

import com.army.study.utils.CommonData;
import com.army.study.utils.LogUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.rpc.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class GameServiceAbility extends Ability {
    private static final String TAG = CommonData.TAG + GameServiceAbility.class.getSimpleName();

    // 创建内部游戏远程类
    private GameRemote remote = new GameRemote();

    @Override
    public void onStart(Intent intent) {
        LogUtil.info(TAG, "GameServiceAbility::onStart");
        super.onStart(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        LogUtil.info(TAG, "GameServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.info(TAG, "GameServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        // 返回游戏远程类
        return remote.asObject();
    }

    @Override
    public void onDisconnect(Intent intent) {
        LogUtil.info(TAG, "GameServiceAbility::onDisconnect");
    }

    /**
     * 游戏远程类
     */
    public class GameRemote extends RemoteObject implements IRemoteBroker {
        private static final int ERR_OK = 0;

        private static final int REQUEST_START_ABILITY = 1;

        private GameRemote() {
            super("GameRemote");
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            LogUtil.info(TAG, "onRemoteRequest......");

            // 设备Id
            String remoteDeviceId = data.readString();
            // 是否为本地端
            boolean isLocal = data.readBoolean();
            // 记录当前要点击的数字
            int flag = data.readInt();
            // 记录开始时间
            long startTime = data.readLong();
            // 当前排列数
            int arrayNumber = data.readInt();
            // 当前列阵数
            int currentCount = data.readInt();

            reply.writeInt(ERR_OK);
            if (code == REQUEST_START_ABILITY) {
                LogUtil.error(TAG, "RemoteServiceAbility::isFirstStart:");
                Intent secondIntent = new Intent();
                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(CommonData.ABILITY_MAIN)
                        .withAction(CommonData.PLAY_PAGE)
                        .build();
                secondIntent.setParam(CommonData.KEY_REMOTE_DEVICEID, remoteDeviceId);
                secondIntent.setParam(CommonData.KEY_IS_LOCAL, isLocal);
                secondIntent.setParam(CommonData.KEY_FLAG, flag);
                secondIntent.setParam(CommonData.KEY_START_TIME, startTime);
                secondIntent.setParam(CommonData.KEY_ARRAY_NUMBER, arrayNumber);
                secondIntent.setParam(CommonData.KEY_CURRENT_COUNT, currentCount);
                secondIntent.setParam(CommonData.KEY_COUNT, currentCount);
                secondIntent.setOperation(operation);
                // 跳转到游戏界面
                startAbility(secondIntent);
            } else {
                // 发送事件
                sendEvent(remoteDeviceId, isLocal, flag, startTime, arrayNumber, currentCount);
            }
            return true;
        }
    }


    private void sendEvent(String remoteDeviceId, boolean isLocal, int flag, long startTime, int arrayNumber, int currentCount) {
        try {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder().withAction(CommonData.GAME_EVENT).build();
            intent.setOperation(operation);
            intent.setParam(CommonData.KEY_REMOTE_DEVICEID, remoteDeviceId);
            intent.setParam(CommonData.KEY_IS_LOCAL, isLocal);
            intent.setParam(CommonData.KEY_FLAG, flag);
            intent.setParam(CommonData.KEY_START_TIME, startTime);
            intent.setParam(CommonData.KEY_ARRAY_NUMBER, arrayNumber);
            intent.setParam(CommonData.KEY_CURRENT_COUNT, currentCount);
            CommonEventData eventData = new CommonEventData(intent);
            // 发布事件
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            LogUtil.error(TAG, "publishCommonEvent occur exception.");
        }
    }

}